package com.huawei.rbac.util;

import com.huawei.rbac.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {

    // session中员工常量
    public static final String  EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";

    // session中员工权限表达式常量
    public static final String  PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";


    public static HttpSession getSession(){

        return ((ServletRequestAttributes) RequestContextHolder.
        getRequestAttributes()).getRequest().getSession();
    }

    //获取当前登录的用户, 将用户设置到session
    public static Employee getCurrentUser(){

        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
}

    //设置当前登录的用户, 将用户设置到session
    public static void setCurrentUser(Employee e){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,e);
    }

    //设置当前用户的权限表达式集合, 共享到session中
    public static void setPermissionExps(List<String> list) {
        getSession().setAttribute(PERMISSION_IN_SESSION, list);
    }

    //获取当前用户的权限表达式集合
    public static List<String> getPermissionExps() {
        return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    }
}
